
import pygame, math, sys, random, time
from EnemyHelicopter import EnemyHelicopter
from Helicopter import Helicopter
from LifeBar import LifeBar
from Bullet import Bullet
from menu import Button
from Screen import Screen
from HighScroeReader import HighScoreReader
from HighScore import HighScore
from Score import Score
from Timer import Timer
#from bossEnemyhelicopter import EnemyHelicopter
#from bossBullet import Bullet
#from Joystickcontrol import Joystickcontrol

def levelLoader(level, dir):
    if level == "rsc/Farmyfarmfarmbro.png":
        print dir
        if dir == "N":
            level = "rsc/backround.png"
        elif dir == "W":
            level = "rsc/Washington.png"
    elif level == "rsc/backround.png":
        if dir == "N":
            level = "rsc/space.png"
        elif dir == "S":
            level = "rsc/Farmyfarmfarmbro.png"
    elif level == "rsc/Washington.png":
        if dir == "E":
            level = "rsc/Farmyfarmfarmbro.png"
        elif dir == "W":
            level = "rsc/berlin.png"
    elif level == "rsc/berlin.png":
        if dir == "E":
            level = "rsc/Washington.png"
    elif level == "rsc/space.png":
        if dir == "S":
            level = "rsc/backround.png" 
        elif dir == "N":
            level = "rsc/blackhole.png"
    elif level == "rsc/blackhole.png":
        if dir == "S":
            level = "rsc/space.png"
    return level
    

   

if pygame.mixer:
    pygame.mixer.init()

pygame.init()
joynum = pygame.joystick.get_count()
if joynum >=1:
    joystick = True
    joy1 = pygame.joystick.Joystick(0)
    joy1.init()
else:
    joystick = False 

clock = pygame.time.Clock()

screenWidth = 800
screenHeight = 600

screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode(screenSize)



player = Helicopter(5, [screenWidth/2, screenHeight-100], screenSize, 5)
enemyHelicopters = []
playerBullets = []
enemyBullets = []
maxEnemyHelicopters = 1
maxBullets = 10
maxEnemyBullets = 10
lifeBar = LifeBar(100, [0,0])
enemies = []
boss = []
bosss = []
maxbosss = 1
bossBullets = []
maxbossBullets = 15
timer = Timer([730, 50])

    #>>>>>> Load Background <<<<<<
background = Screen(["rsc/planes.png"], [0,0], screenSize, 10)
death = Screen(["rsc/scan020.jpg"], [0,0], screenSize, 10)
level = "rsc/Farmyfarmfarmbro.png"
background = pygame.image.load(level)
backgroundRect = background.get_rect()
singleplayer = Button("FIGHT (X)", [250,300], (0, 100, 100))
exit = Button("OR FLIGHT (B)", [250,400], (0, 100, 100))

size = screenSize = screenWidth, screenHeight = background.get_size()
screen = pygame.display.set_mode(size)


bgColor = 225, 255, 255

run  = False

    #>>>>>> Menu <<<<<<
while True:
    while not run and player.living:
        #print run, player.living
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_UP or event.key == pygame.K_w) and not singleplayer.highlighted:
                    singleplayer.highlighted = True
                    exit.highlighted = False
                elif event.key == pygame.K_DOWN or event.key == pygame.K_s and not exit.highlighted:
                    exit.highlighted = True
                    singleplayer.highlighted = False
                elif event.key == pygame.K_SPACE and exit.highlighted == True:
                    sys.exit()
                elif event.key == pygame.K_SPACE and singleplayer.highlighted == True:
                    run = True
            elif event.type == pygame.MOUSEMOTION:
                if singleplayer.collidePt(event.pos):
                    singleplayer.highlighted = True
                    exit.highlighted = False
                elif exit.collidePt(event.pos):
                    singleplayer.highlighted = False
                    exit.highlighted = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if singleplayer.collidePt(event.pos):
                        singleplayer.clicked = True
                        run = True
                    elif exit.collidePt(event.pos):
                        exit.clicked = True
                        sys.exit()
            elif event.type == pygame.JOYBUTTONDOWN:
                if event.button == 2: #-- 'X' Button 
                    singleplayer.clicked = True
                    run = True
                if event.button == 1: #-- 'B' Button 
                    exit.clicked = True
                    sys.exit()
                  

        singleplayer.update((200, 10, 10))
        exit.update((200, 10, 10))
        
        screen.fill(bgColor)
        screen.blit(singleplayer.surface, singleplayer.rect)
        screen.blit(exit.surface, exit.rect)
        #player = Helicopter(10, screenSize, [360, 10])
        pygame.display.flip()
        

    
    #>>>>>> Keyboard stuff <<<<<<
    timer.start()
    while run and player.living:
        startTime = time.time()
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_UP 
                    or event.key == pygame.K_w):
                        player.direction("up")
                elif (event.key == pygame.K_DOWN 
                    or event.key == pygame.K_s):
                        player.direction("down")
                elif (event.key == pygame.K_RIGHT 
                    or event.key == pygame.K_d):
                        player.direction("right")
                elif (event.key == pygame.K_LEFT 
                    or event.key == pygame.K_a):
                        player.direction("left")
                elif (event.key == pygame.K_SPACE):
                    playerBullets += [Bullet([0,-2], player.rect.center,                                     screenSize)]
                    #print "time at event queue", time.time() - startTime
                elif (event.key == pygame.K_q):
                        playerBullets += [Bullet([0,-2], player.rect.center,                                     screenSize)]
                        for eBullet in enemyBullets:
                            bullet.God(eBullet)
            elif event.type == pygame.KEYUP:
                if (event.key == pygame.K_UP 
                    or event.key == pygame.K_w):
                        player.direction("stop up")
                elif (event.key == pygame.K_DOWN 
                    or event.key == pygame.K_s):
                        player.direction("stop down")
                elif (event.key == pygame.K_RIGHT 
                    or event.key == pygame.K_d):
                        player.direction("stop right")
                elif (event.key == pygame.K_LEFT 
                    or event.key == pygame.K_a):
                        player.direction("stop left")  
                elif (event.key == pygame.K_SPACE):
                        player.direction("stop shooting")
                        
            elif event.type == pygame.JOYAXISMOTION:
                #print event.joy, event.axis, event.value
                if event.axis == 0: #x on left stick
                    if event.value >= .2:
                        player.direction("right")
                    elif event.value <= -.2:
                        player.direction("left")
                    else:
                        player.direction("stop right")
                if event.axis == 1: #y on left stick
                    if event.value >= .2:
                        player.direction("down")
                    elif event.value <= -.2:
                        player.direction("up")
                    else:
                        player.direction("stop up")
            elif event.type == pygame.JOYBUTTONDOWN:
                if event.button == 3: #-- 'y' Button 
                    playerBullets += [Bullet([0,-2], player.rect.center,                                     screenSize)]
                    for eBullet in enemyBullets:
                        bullet.God(eBullet) 
                if event.button == 0: #-- 'A' Button 
                    playerBullets += [Bullet([0,-2], player.rect.center,                                     screenSize)] 
            elif event.type == pygame.JOYBUTTONUP:
                if event.button == 0: #-- 'A' Button 
                    player.direction("stop shooting")  
             
                     
                             
        #>>>>>>Generate Enemy Objects<<<<<<
        while len(enemyHelicopters) < maxEnemyHelicopters:
            enemyHelicopterSpeed = [random.randint(1,6),0]
            enemyHelicopterPos = [random.randint(100,screenWidth-100),    
                       random.randint(100,screenHeight/2)]
            enemyHelicopters += [EnemyHelicopter(enemyHelicopterSpeed,
                                                 enemyHelicopterPos, 
                                                 screenSize)]
            
        #>>>Enemies shoot stuff<<<
        if len(enemyBullets) < maxEnemyBullets:
            for enemyHelicopter in enemyHelicopters:
                if random.randint(1,10) == 1:
                    enemyBullets += [Bullet([0,2], enemyHelicopter.rect.center,                                    screenSize)]
        
        #>>>>>>Move Stuff<<<<<<
        for enemyHelicopter in enemyHelicopters:
            enemyHelicopter.move()
        for boss in bosss:
            boss.move()
        for bullet in playerBullets:
            bullet.move()
        for bullet in enemyBullets:
            bullet.move()
        for bullet in bossBullets:
            bullet.move()
        player.move()
        
        """
        for enemyHelicopter in enemyHelicopters:
            if enemyHelicopter.distToPoint(player.rect.center) < enemyHelicopter.attackRadius:
                pX = player.rect.center[0]
                pY = player.rect.center[1]
                zX = enemyHelicopter.rect.center[0]
                zY = enemyHelicopter.rect.center[1]
                            
            if pX > zX and (event.key == pygame.K_SPACE):
                enemyHelicopter.life -=10
            elif pX < zX and (event.key == pygame.K_SPACE):
                enemyHelicopter.life -=10
                        
            if pY > zY and (event.key == pygame.K_SPACE):
                enemyHelicopter.life -=10
            elif pY < zY and (event.key == pygame.K_SPACE):
                enemyHelicopter.life -=10
        """

            
        #>>>>>>Hit Stuff<<<<<<
        #>>>Player Vs. walls<<<
        wall = player.collideWall(screenWidth, screenHeight)
        if wall != False:
            level = levelLoader(level,wall)
            background = pygame.image.load(level)
            backgroundRect = background.get_rect()
            
        
        #print "time at move and wall collide", time.time() - startTime
        
        #>>>Enemies Vs. others<<<
        for enemyHelicopter in enemyHelicopters:
            enemyHelicopter.collideFloor(screenHeight)
            enemyHelicopter.collideWall(screenWidth,screenHeight)
            if enemyHelicopter.collideHelicopter(player):
                player.hurt(enemyHelicopter.damage)
                lifeBar.update(player)
                
        #>>>Enemies Vs. Enemies<<<
        for first in range(0,len(enemyHelicopters)-2):
            for second in range(first+1, len(enemyhelicopters)-1):
                enemyHelicopters[first].collideenemyHelicopter(enemyHelicopters[second])
                
        #>>>Playerbullets Vs. others<<<
        for bullet in playerBullets:
            bullet.collideWall(screenWidth, screenHeight)
            for enemy in enemies:
                bullet.collideEnemy(enemy)
            for eBullet in enemyBullets:
                bullet.collideBullet(eBullet)
        #>>>Enemybullets Vs. others<<<
        for bullet in enemyBullets:
            bullet.collideWall(screenWidth, screenHeight)
            if bullet.collidePlayer(player):
                player.hurt(bullet.damage)
                lifeBar.update(player)
               
        #>>>>>>Remove Dead<<<<<<           
        for enemyHelicopter in enemyHelicopters:
            if enemyHelicopter.living == False:
                enemyHelicopters.remove(enemyHelicopter)
        for bullet in enemyBullets:
            if bullet.living == False:
                enemyBullets.remove(bullet)
        for bullet in playerBullets:
            if bullet.living == False:
                    playerBullets.remove(bullet)

        timer.update()
        #print "time before draw", time.time() - startTime        
        #>>>>>>Draw Stuff<<<<<<  
        screen.fill(bgColor)
        screen.blit(background,backgroundRect)
        screen.blit(player.surface, player.rect)
        for enemyHelicopter in enemyHelicopters:
            #print enemyHelicopter.rect.center
            screen.blit(enemyHelicopter.surface, enemyHelicopter.rect)
        #for boss in bosss:
         #   screen.blit(boss.surface, boss.rect)
        for bullet in playerBullets:
            screen.blit(bullet.surface, bullet.rect)
        for bullet in enemyBullets:
            screen.blit(bullet.surface, bullet.rect)
        #for bossBullet in bossBullets:
         #   screen.blit(bossBullet.surface, bossBullet.rect)
        screen.blit(lifeBar.surface, lifeBar.rect)
        screen.blit(timer.surface, timer.rect)
        if player.living == False:
           #screen.blit(death.surface, death.rect)
           sys.exit()
           
        
        pygame.display.flip()
        clock.tick(30)
        #print clock.get_fps()
        #print "///////////////////////////////////////////////"
sys.exit()

